<?php

/*
 * litchicms
 * Copyright (C) 2010-7-19 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

$_POST[''] = array();

if (!function_exists('Component_ui_photos')) {
    $BRK_GLOBAL['ComponentValidationFunction'][] = 'Component_Validation_photos';
    $BRK_GLOBAL['ComponentEndingFunction'][] = 'Component_Ending_photos';

    function Component_ui_photos($arr=FALSE) {
        global $Db;
        !$arr && exit('Component_ui_photos');
        $photos = array();
        $rs = brk_select_join(
                        'photos,attachment',
                        '{0}.*,{1}.attach_thumb',
                        "INNER JOIN {1} ON {0}.attach_id={1}.attach_id
                 WHERE {0}.photo_coll_id=" . $arr['collection_value']['photo_coll_id'] . "
                 ORDER BY {0}.photo_sort DESC
                 ");
        while ($row = $Db->fetch_array($rs)) {
            $photos[] = $row;
        }

        return "
    <div class='content_group_row'>
        <table class='thetable' border='0' cellpadding='0' cellspacing='1'>
            <tr>
                <td class='td1' bgcolor='#ffffff'>
                    <div>
                        <div class='cname'><b>图集</b>:</div>
                        <div class='cdesc'>在附件里面多选图片,组成图片集.文章标题作为图片集标题,文章内容正文将作为图片集的整体描述,当然你也可以为每一篇图片添加描述</div>
                    </div>
                </td>
                <td class='td2' >
                    <div id='photo_coll_hidden_inputs'>
                    </div>
                    <div id='photo_coll_big_div'>
                    </div>
<script language='javascript' type='text/javascript'>
    AUTORUN.push(function (){
        attachment_photo_coll_add_all(" . json_encode($photos) . ");
    })
</script>
                </td>
            </tr>
        </table>
    </div>
    ";
    }

}


if (!function_exists('Component_buildUiParam_photos')) {

    function Component_buildUiParam_photos($COLLECTION_ID=0, $ONE_DATA=NULL) {
        if (!$COLLECTION_ID || !$ONE_DATA) {
            return array('collection_value' => FALSE);
        }
        if ($ONE_DATA['photo_coll_id'] == 0) {
            $dataarr = array(
                'collection_id' => $COLLECTION_ID,
                'id' => $ONE_DATA['id'],
                'photo_coll_desc' => ""
            );
            $photo_coll_id = brk_insert('photo_coll', $dataarr);
            if ($photo_coll_id) {
                $dataarr = array(
                    'photo_coll_id' => $photo_coll_id,
                );
                if (!brk_update($GLOBALS['COLLECTION_TABLENAME'], $dataarr, "WHERE id=" . $ONE_DATA['id'])) {
                    exit('致命错误!新建图册失败');
                }
                $ONE_DATA['photo_coll_id'] = $photo_coll_id;
            }
        }
        return array('collection_value' => $ONE_DATA);
    }

}


if (!function_exists('Component_Validation_photos')) {

    function Component_Validation_photos() {
        $_attach_id_arr = empty($_POST['Component']['photos']['attach_id']) ? array() : $_POST['Component']['photos']['attach_id'];
        $_attach_sort_arr = empty($_POST['Component']['photos']['attach_sort']) ? array() : $_POST['Component']['photos']['attach_sort'];
        $_attach_desc_arr = empty($_POST['Component']['photos']['attach_desc']) ? array() : $_POST['Component']['photos']['attach_desc'];

        $photo_coll_id = $GLOBALS['ONE_DATA']['photo_coll_id'];

        $_POST['Component']['photos'] = array();

        $rs = brk_select_join(
                        'attachment,attach_relationships',
                        '{0}.*',
                        "INNER JOIN {1} ON {0}.attach_id={1}.attach_id
                 WHERE {1}.collection_id=" . $GLOBALS['COLLECTION_ID'] . "
                 AND {1}.data_id=" . $GLOBALS['RECORD_ID'] . "
                 ");

        if ($rs) {
            $ATTACHMENTS = array();
            while ($row = $GLOBALS['Db']->fetch_array($rs)) {
                $ATTACHMENTS[] = $row['attach_id'];
            }
            foreach ($_attach_id_arr as $value) {
                if (in_array($value, $ATTACHMENTS)) {
                    $_POST['Component']['photos'][] = array(
                        'photo_coll_id' => $photo_coll_id,
                        'attach_id' => $value,
                        'photo_sort' => array_key_exists($value, $_attach_sort_arr) ? $_attach_sort_arr[$value] : 0,
                        'photo_desc' => array_key_exists($value, $_attach_desc_arr) ? $_attach_desc_arr[$value] : '',
                    );
                }
            }

            //删除 附件和选择的图片的差集
            $diff = array_diff($ATTACHMENTS, $_attach_id_arr);
            if (!empty($diff)) {
                $tempwherein = implode(',', $diff);
                brk_delete('photos', "WHERE photo_coll_id=" . $photo_coll_id . " AND attach_id in(" . $tempwherein . ")");
            }
        }

        return TRUE;
    }

}



if (!function_exists('Component_Ending_photos')) {

    function Component_Ending_photos() {

        foreach ($_POST['Component']['photos'] as $value) {
            $one = brk_select_one('photos', '*', "WHERE photo_coll_id=" . $value['photo_coll_id'] . " AND  attach_id=" . $value['attach_id']);
            if ($one) {
                $updatearray = array();
                if ($one['photo_sort'] != $value['photo_sort']) {
                    $updatearray['photo_sort'] = $value['photo_sort'];
                }
                if ($one['photo_desc'] != $value['photo_desc']) {
                    $updatearray['photo_desc'] = $value['photo_desc'];
                }

                if (!empty($updatearray)) {
                    brk_update('photos', $updatearray, "WHERE photo_coll_id=" . $value['photo_coll_id'] . " AND attach_id=" . $value['attach_id']);
                }
            } else {
                $dataarr = array(
                    'photo_coll_id' => $value['photo_coll_id'],
                    'attach_id' => $value['attach_id'],
                    'photo_sort' => $value['photo_sort'],
                    'photo_desc' => $value['photo_desc']
                );
                brk_insert('photos', $dataarr);
            }
        }

        return TRUE;
    }

}

if (!function_exists('Component_buildField_photos')) {

    function Component_buildField_photos() {
        return array('photo_coll_num' => count($_POST['Component']['photos']));
    }

}